﻿using Microsoft.AspNetCore.Mvc;
using mozhi.smarterp.AuthorCenter.Permissions.Groups;
using mozhi.smarterp.AuthorCenter.Permissions.Items;
using mozhi.smarterp.AuthorCenter.Permissions.PermissionApiGrants;
using mozhi.smarterp.AuthorCenter.Permissions.PermissionGrants;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Identity;

namespace mozhi.smarterp.AuthorCenter.Controllers
{
    [RemoteService]
    [Route("api/identity/permission-groups")]
    public class PermissionGroupController : AuthorCenterController
    {
        private IPermissionGroupAppService _appService;

        private IPermissionItemApiGrantAppService _permissionApiGrantAppService;

        public PermissionGroupController(IPermissionGroupAppService appService , IPermissionItemApiGrantAppService permissionApiGrantAppService)
        {
            _appService = appService;
            _permissionApiGrantAppService = permissionApiGrantAppService;
        }

        [HttpGet]
        [Route("{id}")]
        public async Task<PermissionGroupDto> GetAsync(Guid id)
        {
            return await _appService.GetAsync(id);
        }

        [HttpGet]
        public async Task<PagedResultDto<PermissionGroupDto>> GetListAsync(PermissionGroupFilter input)
        {
            return await _appService.GetListAsync(input);
        }

        [HttpPost]
        public async Task<List<PermissionGroupDto>> CreateAsync(List<PermissionGroupCreateDto> inputs)
        {
            return await _appService.CreateAsync(inputs);
        }

        [HttpPut]
        public async Task<List<PermissionGroupDto>> UpdateAsync(List<PermissionGroupUpdateDto> inputs)
        {
            return await _appService.UpdateAsync(inputs);
        }

        [HttpDelete]
        [Route("{id}")]
        public async Task DeleteAsync(Guid id)
        {
            await _appService.DeleteAsync(id);
        }

        [HttpGet]
        [Route("items/{id}")]
        public async Task<PermissionItemDto> GetItemAsync(Guid id)
        {
            return await _appService.GetItemAsync(id);
        }

        [HttpPost]
        [Route("items")]
        public async Task<PermissionItemDto> CreateItemAsync(PermissionItemCreateDto input)
        {
            return await _appService.CreateItemAsync(input);
        }

        [HttpPut]
        [Route("items")]
        public async Task<PermissionItemDto> UpdateItemAsync(PermissionItemUpdateDto input)
        {
            return await _appService.UpdateItemAsync(input);
        }


        [HttpDelete]
        [Route("items/{id}")]
        public async Task DeleteItemAsync(Guid id)
        {
            await _appService.DeleteItemAsync(id);
        }

        [HttpGet]
        [Route("items/{id}/api-grants")]
        public async Task<List<PermissionItemApiGrantDto>> GetItemApiGrantsAsync(Guid id)
        {
            return await _permissionApiGrantAppService.GetListAsync(new PermissionItemApiGrantFilter() { PermissionItemId = id, SkipCount = 0, MaxResultCount =1000});
        }

        [HttpPut]
        [Route("items/{id}/api-grants")]
        public async Task<List<PermissionItemApiGrantDto>> UpdateItemApiGrantsAsync(Guid id, List<PermissionItemApiGrantUpdateDto> inputs)
        {
            return await _permissionApiGrantAppService.UpdateAsync(id , inputs);
        }
    }
}
